const BASEURL5 = 'https://restapi.amap.com/v5/direction/';
const BASEURL3 = 'https://restapi.amap.com/v3/direction/';
const BASEURL4 = 'https://restapi.amap.com/v4/direction/';
const KEY = '017a0586f3f283a1b0e15b9f7841e6d7';

// 获取驾车路径规划
export const getDrivingDirection = (origin, destination) => {
	return new Promise((resolve, reject) => {
		const url = `${BASEURL3}driving?origin=${origin.join(',')}&destination=${destination.join(',')}&extensions=all&key=${KEY}`;
		uni.request({
			url,
			method: 'GET',
			success: (res) => {
				const data = res.data;
				if (data && data.status === '1') {
					resolve(data);
				} else {
					reject(data);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

// 获取骑行路径规划
export const getBicyclingDirection = (origin, destination) => {
	return new Promise((resolve, reject) => {
		const url = `${BASEURL4}bicycling?key=${KEY}&origin=${origin.join(',')}&destination=${destination.join(',')}`;
		uni.request({
			url,
			method: 'GET',
			success: (res) => {
				const data = res.data;
				if (data && data.data) {
					resolve(data.data);
				} else {
					reject(data);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

// 获取电动车骑行路径规划
export const getElectrobikeDirection = (origin, destination) => {
	return new Promise((resolve, reject) => {
		const url = `${BASEURL4}bicycling?key=${KEY}&origin=${origin.join(',')}&destination=${destination.join(',')}`;
		uni.request({
			url,
			method: 'GET',
			success: (res) => {
				const data = res.data;
				if (data && data.data) {
					resolve(data.data);
				} else {
					reject(data);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

// 获取公交路线规划
export const getRransitDirection = (origin, destination, city = '中山') => {
	return new Promise((resolve, reject) => {
		const url = `${BASEURL3}transit/integrated?key=${KEY}&isindoor=0&origin=${origin.join(',')}&destination=${destination.join(',')}&city=${city}&cityd=&strategy=0&nightflag=0`;
		uni.request({
			url,
			method: 'GET',
			success: (res) => {
				const data = res.data;
				if (data) {
					resolve(data);
				} else {
					reject(data);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

// 获取步行路线规划
export const getWalkingDirection = (origin, destination) => {
	return new Promise((resolve, reject) => {
		const url = `${BASEURL3}walking?key=${KEY}&isindoor=0&origin=${origin.join(',')}&destination=${destination.join(',')}&show_fields=true`;
		uni.request({
			url,
			method: 'GET',
			success: (res) => {
				const data = res.data;
				if (data && data.status === '1') {
					resolve(data);
				} else {
					reject(data);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};

// 距离测量
export const getDistanceInfo = (origins, destination) => {
	return new Promise((resolve, reject) => {
		const url = `${BASEURL3}distance?key=${KEY}&origins=${origins.join(',')}&destination=${destination.join(',')}`;
		uni.request({
			url,
			method: 'GET',
			success: (res) => {
				const data = res.data;
				if (data.status === '1') {
					resolve(data);
				} else {
					reject(data);
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};
